package com.qy.herbagemall.controller;

import com.qy.herbagemall.dao.UserDao;
import com.qy.herbagemall.dbutil.Dbutil;
import com.qy.herbagemall.entity.CMO;
import com.qy.herbagemall.entity.User;
import org.apache.ibatis.session.SqlSession;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("/user")
@CrossOrigin
public class UserController {

    public UserDao dao;

    /**
     * 用户登陆
     * @param user
     * @return
     */
    @PostMapping("/login")
    @ResponseBody
    public CMO login(@RequestBody User user){
        CMO cmo = new CMO();
        SqlSession sqls = Dbutil.getSqls();
        this.dao = sqls.getMapper(UserDao.class);
        User user1 = dao.queryUserByTel(user.getTel());
        if (user1 == null){
            cmo.setCode(1);
            cmo.setMsg("用户不存在");
            return cmo;
        }else if ( user.getTel() == null || user.getPassword() == null
                || user.getTel().equals("") || user.getPassword().equals("") ) {
            cmo.setCode(1);
            cmo.setMsg("用户名或密码不能为空");
            return cmo;
        }else if ( !user.getTel().equals(user1.getTel())
                || !user.getPassword().equals(user1.getPassword())) {
            cmo.setCode(1);
            cmo.setMsg("账号或密码错误");
            return cmo;
        }else {
            cmo.setCode(0);
            cmo.setMsg("登录成功");
            return cmo;
        }
    }

    /**
     * 用户注册
     * @param user
     * @return
     */
    @PostMapping("/register")
    @ResponseBody
    public CMO addUserC(@RequestBody User user){
        CMO cmo = new CMO();
        SqlSession sqls = Dbutil.getSqls();
        this.dao = sqls.getMapper(UserDao.class);
//        int id = Dbutil.getIdNum();
//        user.setId(id);
        dao.addUser(user);
        sqls.commit();
        sqls.close();
        cmo.setCode(0);
        cmo.setMsg("添加成功");
        return cmo;
    }

    /**
     * 删除用户
     * @param id
     * @return
     */
    @PostMapping("/delete")
    @ResponseBody
    public CMO deleteUserC(@RequestParam("id") Integer id){
        CMO cmo = new CMO();
        SqlSession sqls = Dbutil.getSqls();
        this.dao = sqls.getMapper(UserDao.class);
        dao.deleteUser(id);
        sqls.commit();
        sqls.close();
        cmo.setCode(0);
        cmo.setMsg("删除成功");
        return cmo;
    }

    /**
     * 修改用户信息
     * @param user
     * @return
     */
    @PostMapping("/update")
    @ResponseBody
    public CMO updateUserC(@RequestBody User user){
        CMO cmo = new CMO();
        SqlSession sqls = Dbutil.getSqls();
        this.dao = sqls.getMapper(UserDao.class);
        dao.updateUser(user);
        sqls.commit();
        sqls.close();
        cmo.setCode(0);
        cmo.setMsg("修改成功");
        return cmo;
    }

    /**
     * 查询全部
     * @return
     */
    @PostMapping("/queryall")
    @ResponseBody
    public CMO queryAllC(){
        CMO cmo = new CMO();
        SqlSession sqls = Dbutil.getSqls();
        this.dao = sqls.getMapper(UserDao.class);
        List<User> list = dao.queryAllUser();
        cmo.setCode(0);
        cmo.setMsg("查询结果");
        cmo.setData(list);
        return cmo;
    }

    /**
     * 通过id查询一个
     * @param user
     * @return
     */
    @PostMapping("/queryone")
    @ResponseBody
    public CMO queryOneC(@RequestBody User user){
        CMO cmo = new CMO();
        SqlSession sqls = Dbutil.getSqls();
        this.dao = sqls.getMapper(UserDao.class);
        user = dao.queryUserByTel(user.getTel());
//        System.out.println(map.get("id"));
        cmo.setCode(0);
        cmo.setMsg("查找成功");
        cmo.setData(user);
        return cmo;
    }

}
